﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BeerScanatronUI
{
    public partial class CreateNewPassword : Form
    {
        private string _newPassword;
        private string _currentPassword;

        public CreateNewPassword(string currentPassword, string title)
        {
            _currentPassword = currentPassword;

            InitializeComponent();
            this.BackColor = Consts.Theme.BackColor;
            txtCurrentPassword.Select();
            this.Text = title;
        }

        public string GetNewPassword()
        {
            return _newPassword;
        }

        private void CreateNewPassword_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult == DialogResult.Cancel)
            {
                // Allow the window to cancel.
                return;
            }
            else if (txtCurrentPassword.Text == _currentPassword
                    && !String.IsNullOrWhiteSpace(txtNewPassword.Text)
                    && txtNewPassword.Text.Length >= Consts.Password.MinPasswordChars
                    && txtNewPassword.Text == txtConfirmPassword.Text)
            {
                // Validation for returning "OK" to caller passed.
                _newPassword = txtNewPassword.Text;
            }
            else
            {
                // Trying to exit with "OK" but validation failed.
                MessageBox.Show("No change occurred.  Either current password was incorrect or new password did not match confirm password."
                    + "  Remember that passwords must be at least " + Consts.Password.MinPasswordChars.ToString() + " characters in length.",
                    "Change not accepted", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                e.Cancel = true;
            }
        }
    }
}
